<?php
namespace Home\Controller;
use Think\Controller;
class ServiceController extends Controller {
    public function index(){
        $uid=session('uid');
        $news=M('fabulous')->order('fabulous desc')->join('news on news.id=fabulous.fs_id')->where(array("satus" => "1"))->select();
        $this->news=$news;
        for ($i = 0; $i < count($news); $i++){
            $news[$i]["code"] = $i + 1;
        }
        if(!$uid)
        {$this->error('登录已过期!',U("Home/Login/index"));}
        elseif($uid){
            $this->assign('list', $news);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Home/Index/index"));}
    }
    public function release(){
        $uid=session('uid');
        $data['title'] = trim(I('post.title'));
        $data['tid'] = trim(I('post.tid'));
        $data['content'] = trim(I('post.content'));
        $data['n_uid'] = $uid;
        $data['satus'] = '0';
        $data['top'] = '0';
        $data['publishment'] = date('y-m-d', time());


        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/images/pic/'; // 设置附件上传根目录
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        $data['n_pic']=$info['savepath'].$info['savename'];
        $result=M('news')->where(array('title'=>$data['title']))->find();
        if($result)
            $this->error('标题重复！', U("Service/index"));
        else{
            M('news')->add($data);
            $url=U('Service/index');
            header("Location: $url");
            exit(0);
        }

    }
}